using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Threading;
namespace MicroMsg.UI.Util
{
	public class UITaskManager
	{
		private Queue<UITask> mQueueTask;
		private UITaskDlg mAllTaskFinishHandler;
		private DispatcherTimer mTimer;
		public UITaskManager(List<UITask> listTasks, UITaskDlg finishHandler = null)
		{
			if (listTasks != null)
			{
				this.mQueueTask = new Queue<UITask>(listTasks);
			}
			this.mAllTaskFinishHandler = finishHandler;
		}
		public void AddTask(UITask task)
		{
			if (this.mQueueTask == null)
			{
				this.mQueueTask = new Queue<UITask>();
			}
			this.mQueueTask.Enqueue(task);
		}
		public static void DoTaskLater(UITaskDlg task, uint nDelayTime = 0u)
		{
			List<UITask> list = new List<UITask>();
			list.Add(new UITask(task, 0u));
			List<UITask> listTasks = list;
			UITaskManager uITaskManager = new UITaskManager(listTasks, null);
			uITaskManager.StartTasks(nDelayTime);
		}
		private void setNextTaskTime(uint duaration)
		{
			if (this.mTimer == null)
			{
				this.mTimer = new DispatcherTimer();
				this.mTimer.add_Tick(new EventHandler(this.mTimer_Tick));
			}
			this.mTimer.set_Interval(TimeSpan.FromMilliseconds(duaration));
			this.mTimer.Start();
		}
		private void mTimer_Tick(object sender, EventArgs e)
		{
			if (this.mTimer != null)
			{
				this.mTimer.Stop();
			}
			Deployment.get_Current().get_Dispatcher().BeginInvoke(delegate
			{
				this.doNextTask();
			});
		}
		private void doTask(UITask uitask)
		{
			if (uitask != null)
			{
				uitask.DoTask();
				if (uitask.mDuaration <= 0u)
				{
					this.doNextTask();
					return;
				}
				this.setNextTaskTime(uitask.mDuaration);
			}
		}
		private void doNextTask()
		{
			if (this.mQueueTask != null && this.mQueueTask.get_Count() != 0)
			{
				UITask uitask = this.mQueueTask.Dequeue();
				this.doTask(uitask);
				return;
			}
			if (this.mAllTaskFinishHandler != null)
			{
				this.mAllTaskFinishHandler();
			}
		}
		public void StartTasks(uint nStartTime = 0u)
		{
			if (nStartTime <= 0u)
			{
				this.doNextTask();
				return;
			}
			this.setNextTaskTime(nStartTime);
		}
		public void StopTasks()
		{
			if (this.mQueueTask != null)
			{
				this.mQueueTask.Clear();
				this.mQueueTask = null;
			}
			if (this.mTimer != null)
			{
				this.mTimer.Stop();
				this.mTimer.remove_Tick(new EventHandler(this.mTimer_Tick));
				this.mTimer = null;
			}
		}
	}
}
